package com.example.employee.restfulapi.controller;

import com.example.employee.restfulapi.entity.Company;
import com.example.employee.restfulapi.entity.Employee;
import com.example.employee.restfulapi.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class EmployeeController {
    //在此处完成Employee API
    @Autowired
    EmployeeRepository employeeRepository;


    @GetMapping("/employees")
    public List<Employee> getAllEmployees(){
        return employeeRepository.findAll();
    }

    @GetMapping("/employees/{id}")
    public Employee getEmployeeById(@PathVariable int id){
        return employeeRepository.findOne((long) id);
    }

    //    @GetMapping("/companies/{id}/employees")
//    public List<Employee> getEmployeesByCompany(@PathVariable int id){
//        return employeeRepository.findOne(id)
//    }
    @GetMapping(" /employees/page/{page}/pageSize/{pageSize} ")
    public Page<Employee> getEmployeesByPage(@PathVariable("page") int page, @PathVariable("pageSize") int pageSize){
        System.out.println("aaa");
        return employeeRepository.findAll(new PageRequest(page,pageSize));
    }

    @PostMapping("/post/employees")
    public Employee addCompany(Employee employee){
        return employeeRepository.save(employee);
    }

//    @PutMapping("/put/employees")
//    public Employee updateEmployee(Employee employee){
//        employee.set(employee.getCompanyName());
//        employee.setEmployeesNumber(employee.getEmployeesNumber());
//        employee.setId(company.getId());
//        return employeeRepository.save(company);
//    }
    @DeleteMapping("/delete/employees/{id}")
    public void  deleteEmployee(@PathVariable Long id){
        employeeRepository.delete(id);
    }
}
